
//*******************************左侧方法****************************
//1.设置传感器列表-----Left
function initSensorList() {
    /*let labcode = $.cookie('labcode');*/
   let labcode = "Haierrefrigerator20151214";
    let nowprimarykey = "2019-04-28 16:12:001";
   /* var nowprimarykey = $.cookie("nowprimarykey");
    var labcode = $.cookie("labcode");*/

    /*let nowprimarykey = $.cookie('nowprimarykey');*/
    let topBasePath=getTopBasePath(top.basePath);
    $.ajax({
        type: "get",
        async: false,//设置为同步请求,防止接收不到数据
        url: "http://localhost/consumer/CurveInfoAPI/getSensorInfoByCondition",
        data: {
            labcode: labcode,
            primaryKey: nowprimarykey
        },
        success: function (msg) {
            top.sensorSize = msg.length;
            console.log("传感器数量为" + top.sensorSize);
            $('.senListLeft li').remove();
            var str = "";
            for (var i = 0; i < msg.length; i++) {

                //获取最新的一条传感器数据
                let sensorvalue=getNewSensorValues(nowprimarykey,msg[i].sensorno);
                //拼接颜色的class
                let color = "sensorColor color" + msg[i].colorno;
                str += '<li>';
                //注意:只有cs端选择了传感器,Bs端选择后才能显示
                if ("1" == msg[i].visible) {
                    //获取选中传感器的测试数据,并存在session Storage中
                    let sensorValues=sessionStorage.getItem("sensorno"+msg[i].sensorno+"Values");
                    if(sensorValues=="undefined"||sensorValues==null||sensorValues.length<5){
                        //根据传感器编号获取传感器数据并放入sessionStorage中
                        sensorValues=getSensorDataList(msg[i].sensorno);
                        sessionStorage.setItem("sensorno"+msg[i].sensorno + "Values", JSON.stringify(sensorValues));
                        //将传感器的数据放入到indexedDB中
                        //Jquery合并两个json
                        var id={'key':"sensorno"+msg[i].sensorno + "Values"};
                        //注意：此处需要将json字符串转化为json对象
                        var sensorValuesJson=eval('(' + JSON.stringify(sensorValues) + ')');
                        var indexedDBJson=$.extend({}, id,sensorValuesJson);
                        // method.add(indexedDBJson);
                    }

                    str += "<input id='sensorno"+msg[i].sensorno+"' class='checkbox' type='checkbox' checked='checked' onclick='parent.paintSensorCurveLine("+msg[i].sensorno+")'>" +
                        "<input  type='hidden' value='"+msg[i].sensorno+"'>"+
                        "<input class='" + color + "' disabled='disabled' ></input>" +
                        "<label class='sensorName'>"+msg[i].sensorno+":"+ msg[i].name + "</label>" +
                        "<label class='sensorValue'>"+sensorvalue + "</label>";
                } else {
                    //全部放入浏览器缓存中会导致sessionStorage空间不足
                    //获取选中传感器的测试数据,并存在session Storage中
                    // let sensorValues=sessionStorage.getItem("sensorno"+msg[i].sensorno+"Values");
                    // if(sensorValues=="undefined"||sensorValues==null||sensorValues.length<5){
                    //     //根据传感器编号获取传感器数据并放入sessionStorage中
                    //     sensorValues=getSensorDataList(msg[i].sensorno);
                    //     sessionStorage.setItem("sensorno"+msg[i].sensorno + "Values", JSON.stringify(sensorValues));
                    // }


                    str += "<input id='sensorno"+msg[i].sensorno+"' class='checkbox' type='checkbox' onclick='parent.paintSensorCurveLine("+msg[i].sensorno+")'>" +
                        "<input  type='hidden' value='"+msg[i].sensorno+"'>"+
                        "<input class='" + color + "' disabled='disabled'></input>" +
                        "<label class='sensorName'>" +msg[i].sensorno+":"+ msg[i].name + "</label>" +
                        "<label class='sensorValue'>" + sensorvalue + "</label>";
                }

                str += '</li>';
                //将传感器的颜色信息放入cookie中
                //$.cookie(msg[i].sensorno+"color", color);
                // 将传感器的颜色信息放入sessionStorage中
                sessionStorage.setItem(msg[i].sensorno + "color", color);
                //保存数据加载状态
                if(i==msg.length-1){
                    //根据传感器查询最长开测时间
                    getSensorDataList(msg[i].sensorno);
                    //将数据加载完毕的状态保存到sessionStorage中
                    console.log("左侧传感器信息加载完毕.");
                    sessionStorage.setItem("sensorValueStatus", "finish");
                }
            }
            $(".senListLeft").html(str);
        },
        error: function (e) {
            alert("网页发生错误：", e, "error");
        }
    });
}

//2.根据nowprimarykey获取所有传感器的最新一条数据
function getNewSensorValues(primarykey,sensorno){
    let topBasePath=getTopBasePath(top.basePath);
    let sensorvalue="";
    //如果sessionStorage中存在,则从sessionStorage中取,不存在则发送请求
    let sensorvalues=sessionStorage.getItem("newOneTestData");
    if(sensorvalues!=""&&sensorvalues!=undefined){
        //从json数组中查找
        let sensorInfo = eval(sensorvalues).filter((s) => {
            return s.sensorno == sensorno;
        });
        if(sensorInfo[0]!=undefined){
            return sensorInfo[0].sensorvalue+sensorInfo[0].sensorunit;
        }
    }
    $.ajax({
        type: "get",
        async: false,//设置为同步请求,防止接收不到数据
        url: "http://localhost/consumer/CurveInfoAPI/getNewOneTestData",
        data: {
            primaryKey: primarykey,
            sensorNo:sensorno
        },
        success: function (msg) {
            //将最新一条传感器数据保存在sessionStorage中
            if(msg.length>0){
                sessionStorage.setItem("newOneTestData", JSON.stringify(msg));
            }
            for (let i = 0; i < msg.length; i++) {
                // var domSenListLeft = $(".senListLeft li");
                // for(let j=1;j<=domSenListLeft.length;j++){
                //     let sensorno=$("#senListLeft li:eq("+j+") input:eq(2)").val();
                //     if(sensorno==msg[i].sensorno){
                //        return msg[i].sensorvalue;
                //     }
                // }
                if(sensorno==msg[i].sensorno){
                    sensorvalue= msg[i].sensorvalue+" "+msg[i].sensorunit;
                    break;
                }
            }
        },
        error: function (e) {
            alert("网页发生错误：", e, "error");
        }
    });
    return sensorvalue;
}

//3.根据传感器编号获取该传感器的数据列表
function getSensorDataList(sensorno) {
    let sensorValues="";
    //yyi 修改 现在使用labcode 跟primaryKey都是用写死的形式
    let labcode = "Haierrefrigerator20151214";
    let nowprimarykey="2019-04-28 16:12:001";
   /* let labcode= $.cookie('labcode');
    let nowprimarykey= $.cookie('nowprimarykey');*/
    let topBasePath=getTopBasePath(top.basePath);
    //let sensorcolor= $.cookie(sensorno+'color');
    //let sensorcolor= sessionStorage.getItem(sensorno+'color');
    $.ajax({
        type: "get",
        async: false,//设置为同步请求,防止接收不到数据
        url: "http://localhost/consumer/CurveInfoAPI/getSensorTestDataNow",
        data:{
            labcode:labcode,
            primaryKey:nowprimarykey,
            sensorno:sensorno
        },
        success: function (msg) {
            if(msg.length>0){
                top.sensorDataSize=msg[0].sensorvalues.length;
                console.log("传感器"+sensorno+"数据数量为"+top.sensorDataSize);
                //方案1:将总测试时间放在全局变量中(以json数组的形式)
                parent.timeLong=msg[0].totaltime;
                //方案2:将总测试时间放在sessionStorage中
                sessionStorage.setItem("timeScale", msg[0].totaltime);
                if($.cookie('topXlimit')==undefined||$.cookie('topXlimit')==""){
                    sessionStorage.setItem("timeLong", msg[0].totaltime);
                }else{
                    //将timeLong与topXlimit-bottomXlimit的值保持一致
                    sessionStorage.setItem("timeLong", $.cookie('topXlimit')-$.cookie('bottomXlimit'));
                }
                console.log("传感器"+sensorno+"最后一条测试数据的时间为"+parent.timeLong);
                //判断json数组中数据是否已经存在
                let sensorInfo = parent.sensorValues.filter((s) => {
                    return s.sensorno == sensorno;
                });
                if(sensorInfo=="undefined"||sensorInfo==null){
                    parent.sensorValues.push({"sensorno":sensorno,"sensorinfo":msg[0]});
                }
                sensorValues=msg[0];
            }
        },
        error: function (e) {
            alert("网页发生错误：", e, "error");
        }
    });

    return sensorValues;
}




$(document).ready(function () {

    //1.设置传感器列表(延迟1秒后执行)
    setTimeout(function() {
    initSensorList();
    }, 1000);
});

